// ViewOutput.cpp : implementation file
//

#include "stdafx.h"
#include "TravianAutoTask.h"
#include "ViewOutput.h"
#include "ThreadSchedule.h"

// CViewOutput

IMPLEMENT_DYNCREATE(CViewOutput, CRichEditView)

CViewOutput::CViewOutput()
{

}

CViewOutput::~CViewOutput()
{
}

BEGIN_MESSAGE_MAP(CViewOutput, CRichEditView)
	ON_MESSAGE (WM_USERMSG_NEWEVENTLOG,	&CViewOutput::OnNewEventLog)	
	ON_COMMAND(ID_FILE_FONT,			&CViewOutput::OnFileFont)
END_MESSAGE_MAP()


// CViewOutput diagnostics

#ifdef _DEBUG
void CViewOutput::AssertValid() const
{
	CRichEditView::AssertValid();
}

#ifndef _WIN32_WCE
void CViewOutput::Dump(CDumpContext& dc) const
{
	CRichEditView::Dump(dc);
}
#endif
#endif //_DEBUG


// CViewOutput message handlers
LRESULT CViewOutput::OnNewEventLog (WPARAM wParam, LPARAM lParam)
{
	long nNewEvtCnt = (long)lParam;
	EVENTLOG* pNewEvt = new System::EventLog::EVENTLOG[nNewEvtCnt];

	g_TravianEvtLog.FetchEvt(pNewEvt, nNewEvtCnt);
	CString sLog, sTemp;
	for (int i=0; i<nNewEvtCnt; i++)
	{
		g_TravianEvtLog.FormatEvtString( (pNewEvt+i)->nEvtLevel, (pNewEvt+i)->sEvt, sTemp);
		sTemp += _T("\n");
		sLog += sTemp;
	}
	delete [] pNewEvt;

//	m_sRichEditNewContent += sLog;
//	EDITSTREAM es;
//	es.dwCookie = (DWORD_PTR) (&m_sRichEditNewContent);
//	es.pfnCallback = EditStreamInCallback; 
//#ifndef UNICODE
//	theEdit.StreamIn(SF_TEXT, es);
//#else
//	theEdit.StreamIn(SF_TEXT | SF_UNICODE, es);
//#endif

	CRichEditCtrl& theEdit = GetRichEditCtrl();
	int nOldLines = theEdit.GetLineCount();
	long lInsertionPoint = theEdit.GetWindowTextLength();
	// Replace selection. Because we have nothing 
	// selected, this will simply insert
	// the string at the current caret position.
	theEdit.SetSel(lInsertionPoint,-1);
	theEdit.ReplaceSel(sLog);

	// Get new line count
	int nNewLines = theEdit.GetLineCount();
	// Scroll by the number of lines just inserted
	int nScroll = nNewLines - nOldLines;
	theEdit.LineScroll(nScroll);

	return 0;
}
void CViewOutput::Init()
{

	g_TravianEvtLog.SetOutputWnd(m_hWnd);
	g_TravianEvtLog.OpenLogFile();

	CRichEditCtrl& theEdit = GetRichEditCtrl();
	// Set Rich Edit background color
	//PostMessage(EM_SETBKGNDCOLOR, 0, RGB(204, 255, 204));
	PostMessage(EM_SETBKGNDCOLOR, 0, g_crBkgnd);	
	//theEdit.SetMargins ();
	//SetReadOnly();
	PostMessage(EM_SETREADONLY , TRUE , 0);

	ZeroMemory(&m_cfDefault, sizeof(m_cfDefault));
	m_cfDefault.cbSize = sizeof (CHARFORMAT);
	m_cfDefault.dwMask =	CFM_CHARSET | CFM_SIZE | CFM_FACE | CFM_BOLD | CFM_ITALIC | 
							CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
	m_cfDefault.bCharSet = DEFAULT_CHARSET;
	m_cfDefault.dwEffects = CFE_AUTOCOLOR;;
	m_cfDefault.yHeight = 10*20; // 10 pt;
	m_cfDefault.bPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
	LPCTSTR lpszFontName = _T("Arial");
	//LPCTSTR lpszFontName = _T("Courier New");
	_tcscpy_s(m_cfDefault.szFaceName, _tcslen(lpszFontName)+1 , lpszFontName);
	theEdit.SetDefaultCharFormat(m_cfDefault);


	//g_TravianEvtLog.AddEvt(EVTLEVEL_INFO, _T("test"));
}

void CViewOutput::OnFileFont()
{
	// TODO: Add your command handler code here

	CRichEditCtrl& theEdit = GetRichEditCtrl();

	CFontDialog dlgFont(m_cfDefault);
	int ret = (int)dlgFont.DoModal();
	if(IDOK == ret)
	{
		dlgFont.GetCharFormat(m_cfDefault);
		theEdit.SetDefaultCharFormat(m_cfDefault);
	}
	
	//g_TravianEvtLog.AddEvt(EVTLEVEL_INFO, _T("test"));

}
